package wnhz.com.business.shareshop.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.shareshop.bean.JubaoBean;
import wnhz.com.business.shareshop.bean.PingJiaDetailsBean;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBar;
//共享商城举报原因页面
public class JUbaoActivityS extends BaseActivity implements  ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.rl_jubao)
    RecyclerView rlJubao;
    @BindView(R.id.tv_jubao)
    TextView tvJubao;
    private List<JubaoBean.InfoBean.ReportListBean> datalist = new ArrayList<>();
    private List<String> reson = new ArrayList<>();
    private String commentId,type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jubaos);
        ButterKnife.bind(this);
        actionbar.setData("举报原因", R.drawable.ic_lefft_white, null, 0, "", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        if(null!=getIntent()){
            commentId = getIntent().getStringExtra("commentId");
            type = getIntent().getStringExtra("type");
        }
        getData();
    }


    private void getData() {
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        XUtil.Post(Url.GOODS_REPORT_LIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.getString("re");
                    if ("1".equals(re)) {
                        Gson gson = new Gson();
                        JubaoBean item = gson.fromJson(result, JubaoBean.class);
                        datalist = item.getInfo().getReport_list();
                        setdata();
                    }else  if ("-1".equals(jsonObject.getString("result"))){
                        MyApplication.getInstance().gotoLoginActivity();
                    }else{
                        showDialog(jsonObject.getString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
    /**
     * 加载数据
     */
    private void setdata() {
        rlJubao.setLayoutManager(new LinearLayoutManager(JUbaoActivityS.this,LinearLayoutManager.VERTICAL,false));
        rlJubao.setAdapter(new BaseRVAdapter(JUbaoActivityS.this,datalist) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.layout_jubao_item;
            }

            @Override
            public void onBind(final BaseViewHolder holder, final int position) {
                 holder.getTextView(R.id.tv).setText(datalist.get(position).getName());
                 holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                         if( holder.getImageView(R.id.img).isSelected()){
                             reson.remove(datalist.get(position).getId()+"-");
                             holder.getImageView(R.id.img).setBackgroundResource(R.drawable.ic_pay_weixuanz);
                             holder.getImageView(R.id.img).setSelected(false);
                         }else {
                             reson.add(datalist.get(position).getId()+"-");
                             holder.getImageView(R.id.img).setBackgroundResource(R.drawable.ic_xingbie_xuanz);
                             holder.getImageView(R.id.img).setSelected(true);
                         }
                     }
                 });
            }

        });

    }
    private void postJubao() {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i <reson.size() ; i++) {
            str.append(reson.get(i));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApplication.getToken());
        map.put("commentId", commentId);
        map.put("type", type);
        map.put("reportCates", str.substring(0,str.length()-1));
        XUtil.Post(Url.GOODS_COMMON_REPORT, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.getString("re");
                    if ("1".equals(re)) {
                        MyToast("举报成功");
                        JUbaoActivityS.this.finish();
                    }else  if ("-1".equals(jsonObject.getString("result"))){
                        MyApplication.getInstance().gotoLoginActivity();
                    }else{
                        showDialog(jsonObject.getString("info"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }
        });
    }
    @OnClick(R.id.tv_jubao)
    public void onViewClicked() {
        if(reson.size()==0){
            MyToast("请选择举报类型");
            return;
        }
        postJubao();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {


    }

    @Override
    public void onEditTextClick() {

    }
}
